import random
import time
import sys


# import nltk
# from nltk.corpus import words

# # 下载单词列表
# nltk.download('words')

# # 定义要检查的字符串
# input_string = "apple"

# # 检查字符串是否在单词列表中
# if input_string in words.words():
#     print(f"{input_string} is an English word.")
# else:
#     print(f"{input_string} is not an English word.")



# =================================================
from nltk.corpus import wordnet as wn

def CheckEnglishWord(word):
    synsets = wn.synsets(word, lang='eng')
    if not synsets:
        print("Error not an exist English Vocabulary")
        return False

    definition = synsets[0].definition()
    examples = synsets[0].examples()

    print(f"================================================================================")
    print(f"Definition: {definition}")
    print(f"Sentence:   {examples}")
    print(f"================================================================================")

    return True


def CheckAnswerOK(strWord, letter):
    strWord = strWord.strip()

    if not strWord.isalpha():
        print("Must be a exist word, %s is not" %(strWord))
        return False, letter

    if not strWord.startswith(letter):
        print("A word must start with %c, word %s is not" %(letter, strWord))
        return False, letter

    global answerSet

    if strWord in answerSet:
        print("Word %s have already answered" %(strWord))
        return False, letter
    
    if not CheckEnglishWord(strWord):
        return False, letter

    answerSet.add(strWord)
    lastLetter = strWord[-1]
    return True, lastLetter
    
# global
answerSet = set()

# Main
if __name__ == '__main__':
    # Choose the first letter of the first word
    letters = 'abcdefghijklmnopqrstuvwxyz'
    nextLetter = random.choice(letters)
    print(nextLetter)

    nChance = 3
    
    while (1):
        print(" ------ Still have [%d] Chance(s) ------" %nChance)
        strWord = input("Please input a word starts with letter %c : " %(nextLetter) )
        bOK, nextLetter = CheckAnswerOK(strWord, nextLetter)
        if not (bOK):
            nChance = nChance -1
            print("Error nChance - 1, only have %d chance(s)" %nChance)

        if (nChance <= 0):
            print("Game Over")
            sys.exit()
        

